<?php

namespace App\Http\Controllers\staff;

use Illuminate\Http\Request;
use DB;
use App\Http\Controllers\Controller;

class StoreController extends Controller
{
    //店铺列表页
    public function getIndex(Request $request){
        $staff_id = $request -> cookie('staffInfo')['id'];
        //查询管理区域
        $areaArr = DB::table('area_staff as as')
            -> leftJoin('area as a','a.id','=','as.area_id')
            -> where('staff_id',$staff_id)
            -> get();
        foreach($areaArr as $k=>$v){
            $areaArr[$k]['roomArr'] = DB::table('room')
                -> where('area_id',$v['area_id'])
                -> get();
        }
        //dd($areaArr);
        return view('staff.store.index',['areaArr'=>$areaArr]);
    }

    //渲染店铺信息接口
    public function postContract(Request $request){
        $room_id = $request -> input('id');
        $data = DB::table('contract') -> where('room_id',$room_id) -> first();
        return json_encode($data);
    }

    //添加工单页
    public function getWork(Request $request){
        $room_id = $request -> input('id');
        return view('staff.store.work',['room_id'=>$room_id]);
    }

    //处理添加工单页
    public function postWork(Request $request){
        $info = $request -> except('_token');
        $res = DB::table('contract') -> insert($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        return json_encode($data);
    }

    //添加地址页
    public function getCreate(Request $request){
        $staff_id = $request -> cookie('staffInfo')['id'];
        //查询员工所负责区域
        $areaArr = DB::table('area_staff') -> where('staff_id',$staff_id) -> select('area_id') -> get();
        $areaInfo = DB::table('area') -> whereIn('id',$areaArr) -> get();
        return view('staff.store.create',['areaInfo'=>$areaInfo]);
    }

    //添加处理页
    public function postStore(Request $request){
        $info = $request -> except('_token');
        $info['agent_id'] = $request -> cookie('staffInfo')['agent_id'];
        //添加到room表
        $res = DB::table('room') -> insert($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        return json_encode($data);
    }

    //编辑地址页
    public function getEdit(Request $request){
        //地址id
        $store_id = $request -> input('id');
        //获取地址信息
        $storeInfo = DB::table('room') -> where('id',$store_id) -> first();
        // dd($storeInfo);
        $staff_id = $request -> cookie('staffInfo')['id'];
        //查询员工所负责区域
        $areaArr = DB::table('area_staff') -> where('staff_id',$staff_id) -> select('area_id') -> get();
        $areaInfo = DB::table('area') -> whereIn('id',$areaArr) -> get();
        return view('staff.store.edit',['storeInfo'=>$storeInfo,'areaInfo'=>$areaInfo]);
    }

    //编辑地址信息处理页面
    public function postUpdate(Request $request){
        //地址id
        $store_id = $request -> input('id');
        $info = $request -> except('_token','id');
        //修改地址信息
        $res = DB::table('room') -> where('id',$store_id) -> update($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        return json_encode($data);
    }

    public function postDestroy(Request $request){
        $id = $request -> input('id');
        //删除员工表数据
        $res = DB::table('room') -> delete($id);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        return json_encode($data);
    }

    //删除店铺信息
    public function postDelete(Request $request){
        $id = $request -> input('id');
        //删除员工表数据
        $res = DB::table('contract') -> delete($id);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        return json_encode($data);
    }

}